home *** CD-ROM | disk | FTP | other *** search
/ Windows Expert / Windows Expert.iso / windownt / uupc11yt.zip / UTIL / FMT.C < prev    next >
C/C++ Source or Header  |  1993-04-10  |  5KB  |  158 lines

  1. /*--------------------------------------------------------------------*/
  2. /*    Program:    fmt.c          14 August 1990                       */
  3. /*    Author:     Andrew H. Derbyshire                                */
  4. /*                108 Decatur St, Apt 9                               */
  5. /*                Arlington, MA 02174                                 */
  6. /*    Function:   Format lines into user specified width rows         */
  7. /*    Arguments:  width, input file, output file.                     */
  8. /*--------------------------------------------------------------------*/
  9.  
  10. /*--------------------------------------------------------------------*/
  11. /*                          RCS Information                           */
  12. /*--------------------------------------------------------------------*/
  13.  
  14. /*
  15.  *    $Id: FMT.C 1.2 1993/04/11 00:33:54 ahd Exp $
  16.  *
  17.  *    Revision history:
  18.  *    $Log: FMT.C $
  19.  * Revision 1.2  1993/04/11  00:33:54  ahd
  20.  * Global edits for year, TEXT, etc.
  21.  *
  22.  * Revision 1.1  1992/11/15  04:29:22  ahd
  23.  * Initial revision
  24.  *
  25.  * Revision 1.2  1992/04/27  00:41:11  ahd
  26.  * Add RCS Information
  27.  *
  28.  */
  29.  
  30. static char rcsid[] = "$Id: FMT.C 1.2 1993/04/11 00:33:54 ahd Exp $";
  31.  
  32. #include <stdlib.h>
  33. #include <ctype.h>
  34. #include <string.h>
  35. #include <stdio.h>
  36.  
  37. #include "lib.h"
  38. #include "timestmp.h"
  39.  
  40. /*--------------------------------------------------------------------*/
  41. /*    m a i n                                                         */
  42. /*                                                                    */
  43. /*    main program                                                    */
  44. /*--------------------------------------------------------------------*/
  45.  
  46.  void main( int argc, char *argv[] )
  47.  {
  48.    int width = 0;          /* Width of current line                  */
  49.    int maxwidth = 72;      /* Max width of line allowed              */
  50.    char buf[BUFSIZ];       /* Our I/O buffer                         */
  51.    int punct = 0;          /* Last character in last word was punct  */
  52.    int argx = 1;           /* Current argument being processed       */
  53.    FILE *input;
  54.    FILE *output;
  55.  
  56. /*--------------------------------------------------------------------*/
  57. /*                        Announce our version                        */
  58. /*--------------------------------------------------------------------*/
  59.  
  60.    banner( argv );
  61.  
  62. /*--------------------------------------------------------------------*/
  63. /*                            Handle help                             */
  64. /*--------------------------------------------------------------------*/
  65.  
  66.    if (( argc > 1 ) && equal(argv[1],"-?"))
  67.    {
  68.       printf("Usage:\tfmt\t[-#] infile outfile\n");
  69.       exit(1);
  70.    }
  71.  
  72. /*--------------------------------------------------------------------*/
  73. /*       Get the line width if the user specified it                  */
  74. /*--------------------------------------------------------------------*/
  75.  
  76.     if ((argx < argc) && (*argv[argx] == '-'))
  77.       maxwidth = atoi( argv[argx++] );
  78.  
  79. /*--------------------------------------------------------------------*/
  80. /*                  Get the input file name, if any                   */
  81. /*--------------------------------------------------------------------*/
  82.  
  83.     if (argx == argc)
  84.       input = stdin;
  85.     else {
  86.       input = fopen(argv[argx++],"r");
  87.       if (input == NULL)
  88.       {
  89.          perror(argv[--argx]);
  90.          exit (100);
  91.       }
  92.     }
  93.  
  94. /*--------------------------------------------------------------------*/
  95. /*                  Get the output file name, if any                  */
  96. /*--------------------------------------------------------------------*/
  97.  
  98.     if (argx == argc )
  99.       output = stdout;
  100.     else {
  101.       output = fopen(argv[argx++],"w");
  102.       if (output == NULL)
  103.       {
  104.          perror(argv[--argx]);
  105.          exit( 200 );
  106.       }
  107.     }
  108.  
  109. /*--------------------------------------------------------------------*/
  110. /*                          Process the file                          */
  111. /*--------------------------------------------------------------------*/
  112.  
  113.     while( fgets(buf, BUFSIZ, input) != NULL )
  114.     {
  115.       char *token = strtok(buf, " \t\n");
  116.       if (token == NULL )
  117.       {
  118.          fputc('\n',output);
  119.          width = punct = 0;
  120.       }
  121.       else while(token != NULL)
  122.       {
  123.          register size_t toklen = strlen(token);
  124.          width = toklen + width + 1 + punct;
  125.          if (width > max(maxwidth, toklen + 1))
  126.          {
  127.             fputc('\n',output);
  128.             width = toklen;
  129.             punct = 0;
  130.          }
  131.          else {
  132.             if (width > (toklen + 1))
  133.             {
  134.                fputc(' ',output);
  135.                if (punct)
  136.                   fputc(' ',output);
  137.             }
  138.             else
  139.                width = toklen;
  140.             punct = ispunct( token[toklen - 1] ) ? 1 : 0;
  141.          } /* else */
  142.          fputs(token, output);
  143.          token = strtok(NULL, " \t\n");
  144.       } /* else while */
  145.    } /* while */
  146.  
  147.    if (ferror(input))
  148.    {
  149.       perror(argv[1]);
  150.       clearerr(input);
  151.    }
  152.  
  153.    fclose(input);
  154.    fclose(output);
  155.  
  156.    exit (0);
  157.  } /* main */
  158.